﻿using Competition.App.Web.Models;
using Competition.App.Web.Models.NewsViewModels;
using Competition.App.Web.ResultModels.NewsResultModels;
using Competition.Application.Interfaces;
using Competition.Domain.ValueObjects;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Competition.App.Web.Controllers
{

    public class HomeController : Controller
    {
        private readonly Lazy<IIntroductionService> _introductionService;

        public HomeController(Lazy<IIntroductionService> introductionService)
        {
            _introductionService = introductionService;
        }
        public async Task<IActionResult> Index()
        {
            //取扶持政策
            var v = await _introductionService.Value.FindListAsync(IntroductionEnum.扶持政策);
            var product = v.Select(dto => new NewsResultModel()
            {
                Id = dto.Id,
                AddTime = dto.AddTime.ToString("yyyy-MM-dd"),
                Title = dto.Title,
                Description = dto.Description,
            }).ToList();
            ViewBag.ProductList = product;
            ////取项目库
            //var v2 = await _introductionService.Value.FindListAsync(IntroductionEnum.项目库);
            //var news = v2.Select(dto => new NewsResultModel()
            //{
            //    Id = dto.Id,
            //    AddTime = dto.AddTime.ToString("yyyy-MM-dd"),
            //    Title = dto.Title,
            //    Description = dto.Description,
            //}).ToList();
            //ViewBag.NewsList = news;
            return View();
        }
        // GET: News/Details/5
        public async Task<IActionResult> Details(int id)
        {
            var dto = await _introductionService.Value.FindAsync(id);
            NewsViewModel model = new NewsViewModel()
            {
                Description = dto.Description,
                AddTime = dto.AddTime.ToString("yyyy.M.d"),
                Title = dto.Title,
                Id = dto.Id,
                Html = dto.Html
            };
            return View(nameof(Details), model);
        }
        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
